/*
 * Copyright (C) 2001-2016 Food and Agriculture Organization of the
 * United Nations (FAO-UN), United Nations World Food Programme (WFP)
 * and United Nations Environment Programme (UNEP)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
 * Rome - Italy. email: geonetwork@osgeo.org
 */

package org.fao.geonet.domain;

import org.fao.geonet.entitylistener.MetadataIdentifierTemplateListenerManager;

import javax.persistence.*;

/**
 * An entity representing metadata identifier template. Metadata identifier templates are used to
 * allow users to define custom metadata UUIDs following certain patters.
 *
 * @author Jose García
 */
@Entity
@Table(name = "MetadataIdentifierTemplate")
@Cacheable
@Access(AccessType.PROPERTY)
@EntityListeners(MetadataIdentifierTemplateListenerManager.class)
@SequenceGenerator(name = MetadataIdentifierTemplate.ID_SEQ_NAME, initialValue = 100, allocationSize = 1)
public class MetadataIdentifierTemplate extends GeonetEntity {
    static final String ID_SEQ_NAME = "metadata_identifier_template_id_seq";

    private int _id;
    private String _name;
    private String _template;
    private char _systemProvided = 'n';

    /**
     * Get the id of the metadata identifier template. <p> This is autogenerated and when a new
     * metadata identifier template is created the metadata identifier template will be assigned a
     * new value. </p>
     *
     * @return the id of the metadata identifier template.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = ID_SEQ_NAME)
    @Column(nullable = false)
    public int getId() {
        return _id;
    }

    /**
     * Get the id of the metadata identifier template. <p> This is autogenerated and when a new
     * metadata identifier template is created the metadata identifier template will be assigned a
     * new value. </p> <p> If you want to update an existing MetadataUrnTemplate then you should set
     * this id to the metadata identifier template you want to update and set the other values to
     * the desired values </p>
     *
     * @param id the id of the metadata identifier template.
     * @return this metadata identifier template object
     */
    public MetadataIdentifierTemplate setId(int id) {
        this._id = id;
        return this;
    }

    /**
     * Get the basic/default name of the metadata identifier template. This is non-translated and
     * can be used to look up the metadata identifier template like an id can. <p> This is a
     * required property. <p> There is a max length to the name allowed. See the annotation for the
     * length value </p>
     *
     * @return metadata identifier template name
     */
    @Column(nullable = false, length = 32)
    public String getName() {
        return _name;
    }

    /**
     * Set the basic/default name of the metadata identifier template. This is non-translated and
     * can be used to look up the metadata identifier template like an id can. <p> This is a
     * required property. <p> There is a max length to the name allowed. See the annotation on
     * {@link #getName()} for the length value </p>
     */
    public MetadataIdentifierTemplate setName(String name) {
        this._name = name;
        return this;
    }

    /**
     * Get a template value of the metadata identifier template.
     *
     * @return the description.
     */
    @Column(nullable = false, length = 255)
    public String getTemplate() {
        return _template;
    }

    /**
     * Set the template value.
     *
     * @param template the template value.
     * @return this metadata identifier template object.
     */
    public MetadataIdentifierTemplate setTemplate(String template) {
        this._template = template;
        return this;
    }

    /**
     * Return true if the metadata identifier template is provided by GeoNetwork.
     *
     * @return true if the metadata identifier template is provided by GeoNetwork.
     */
    @Transient
    public boolean isSystemProvided() {
        return Constants.toBoolean_fromYNChar(getSystemProvided_JPAWorkaround());
    }

    /**
     * Set true if the metadata identifier template is provided by GeoNetwork.
     *
     * @param isSystemProvided true if the metadata identifier template is provided by GeoNetwork.
     */
    public void setSystemDefault(boolean isSystemProvided) {
        setSystemProvided_JPAWorkaround(Constants.toYN_EnabledChar(isSystemProvided));
    }

    /**
     * For backwards compatibility we need the enabled column to be either 'n' or 'y'. This is a
     * workaround to allow this until future versions of JPA that allow different ways of
     * controlling how types are mapped to the database.
     */
    @Column(name = "isprovided", length = 1, nullable = false, columnDefinition = "Char(1) default 'n'")
    protected char getSystemProvided_JPAWorkaround() {
        return _systemProvided;
    }

    /**
     * Set the enabled column value as either Constants.YN_ENABLED or Constants.YN_DISABLED
     *
     * @param _default either Constants.YN_ENABLED for true or Constants.YN_DISABLED for false
     */
    protected void setSystemProvided_JPAWorkaround(char _default) {
        this._systemProvided = _default;
    }

}
